package j2se.Datagram;

import java.io.IOException;
import java.net.*;

/**
 * Created by jingqing.zhou on 2015/6/17.
 */
public class UDPClinent {
    public static void main(String[] args) throws IOException {

       //1:定义服务器的地址，端口号
        InetAddress address = InetAddress.getByName("localhost");
        int port = 8999;
        byte[] data = "用户名：admin;密码：123".getBytes();
        //2:封装数据报
        DatagramPacket packet = new DatagramPacket(data,data.length,address,port);
        //3:定义负责发送数据的socket
        DatagramSocket socket = new DatagramSocket();
        //4:发送数据
        socket.send(packet);

        //封装一个空的数据报，用于接收服务器端的数据报
        byte[] data2 = new byte[1024];
        DatagramPacket packet2 = new DatagramPacket(data2,data2.length);

        socket.receive(packet2);

        String repy =  new String(data2,0,packet2.getLength());
        System.out.println("我是客户端，服务器说："+repy);

        socket.close();

    }
}
